package com.leetroll.app.controller;

import com.leetroll.api.service.UserService;
import com.leetroll.api.vo.UserVO;
import com.leetroll.app.utils.AppJwtTokenUtil;
import com.leetroll.common.bean.ApiResponse;
import com.leetroll.common.entity.User;
import com.leetroll.common.enums.EBucket;
import com.leetroll.common.utils.*;
import com.leetroll.common.validator.group.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;

import static com.leetroll.common.enums.ECode.TOKEN_LOSE_EFFECT;
import static com.leetroll.common.utils.AliyunOSSUtil.GetUrl;

@RestController
@RefreshScope
@RequestMapping("/user")
@Api(value = "用户相关接口", tags = {"用户相关接口"})
@Slf4j
public class UserController {

    private final UserService userService;
    private final AppJwtTokenUtil jwtTokenUtil;

    @Autowired
    public UserController(UserService userService, AppJwtTokenUtil jwtTokenUtil) {
        this.userService = userService;
        this.jwtTokenUtil = jwtTokenUtil;
    }

    /**
     * 获取用户信息
     *
     * @author qdy
     **/
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping("/get")
    public ApiResponse<UserVO> getUserInfos(HttpServletRequest request) {
        Long userId = jwtTokenUtil.getUserId(request);
        if (userId == null) {
            return ApiResponse.FAIL(TOKEN_LOSE_EFFECT, "请重新登录");
        }
        User userInfo = userService.getUser(userId);
        return ApiResponse.OK(UserVO.fromUser(userInfo));
    }

    @ApiOperation(value = "编辑用户", notes = "编辑用户", response = String.class)
    @PostMapping("/update")
    public ApiResponse<User> updateUser(@Validated({Update.class}) @RequestBody UserVO userVO, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        Boolean isUpdate = userService.editUser(userVO);
        if (isUpdate) {
            return ApiResponse.OK();
        }
        return ApiResponse.FAIL();
    }

    @ApiOperation(value = "更新用户头像", notes = "更新用户头像", response = String.class)
    @PostMapping("/avatar/update")
    public ApiResponse<User> updateAvatar(HttpServletRequest request, @Validated({Update.class}) @RequestBody UserVO userVO, BindingResult result) {
        Long userId = jwtTokenUtil.getUserId(request);
        userVO.setId(userId);

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        Boolean isUpdate = userService.updateAvatar(userVO);
        if (isUpdate) {
            return ApiResponse.OK();
        }
        return ApiResponse.FAIL();
    }

    @ApiOperation(value = "更新用户头像", notes = "更新用户头像", response = String.class)
    @PostMapping("/avatar/upload")
    public ApiResponse<String> uploadAvatar(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        Long userId = jwtTokenUtil.getUserId(request);

        //upload file to aliyun
        String path = "uploads/user/" + userId + "/avatar" + file.getOriginalFilename();
        AliyunOSSUtil.upload(path, file.getInputStream());

        //get aliyun link
        String url = GetUrl(path, 10);
        UserVO userVO = new UserVO();
        userVO.setId(userId);
        userVO.setAvatar(url);
        userService.updateAvatar(userVO);

        return ApiResponse.OK(url);
    }

    @ApiOperation(value = "用户背景图片更换", notes = "用户背景图片更换", response = String.class)
    @PostMapping("/bg/update")
    public ApiResponse<User> updateBackImg(HttpServletRequest request, @Validated({Update.class}) @RequestBody UserVO userVO) {
        Long userId = jwtTokenUtil.getUserId(request);
        userVO.setId(userId);

        // 参数校验
        Boolean isUpdate = userService.updateBackImg(userVO);
        if (isUpdate) {
            return ApiResponse.OK();
        }
        return ApiResponse.FAIL();
    }

}